<?php
namespace app\website\model;

use app\BaseModel;
use think\facade\Db;

class StarList extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_web_star_list';

    protected $append = [

        'imgs'
    ];

    /**
     * @author chenniang
     * @DataTime: 2020-12-24 18:09
     * @功能说明:
     */
    public function getImgsAttr($value,$data){

        if(!empty($value)){

            return explode(',',$value);

        }

    }




    /**
     * @author chenniang
     * @DataTime: 2020-12-25 09:59
     * @功能说明:首页列表
     */
    public function indexList($dis,$type,$page=10){

        if(!empty($type)){

            $data = $this->alias('a')
                ->join('longbing_card_web_star_connect 1','a.id = 1.star_id ','left')
                ->join('longbing_card_web_star_connect 2','a.id = 2.star_id ','left')
                ->join('longbing_card_web_star_connect 3','a.id = 3.star_id ','left')
                ->join('longbing_card_web_star_connect 4','a.id = 4.star_id ','left')
                ->where($dis)
                ->field('a.*')
                ->group('a.id')
                ->order('a.top desc,a.id desc')
                ->paginate($page)
                ->toArray();
        }else{

            $data = $this->alias('a')
                ->where($dis)
                ->field('a.*')
                ->group('a.id')
                ->order('a.top desc,a.id desc')
                ->paginate($page)
                ->toArray();

        }

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-08 10:51
     * @功能说明:列表
     */
    public function startList($dis,$page=10){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-09 17:05
     * @功能说明:配置详情
     */
    public function startInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)? $data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:配置添加
     */
    public function startAdd($data){

        $data['create_time'] = time();

        $data['update_time'] = time();

        $recommend = $data['recommend'];

        unset($data['recommend']);

        $choose    = $data['choose'];

        unset($data['choose']);

        $data['imgs'] = !empty($data['imgs'])?implode(',',$data['imgs']):'';

        if(isset( $data['cate_id'])){

            $data['cate_id'] = array_pop($data['cate_id']);
        }

        Db::startTrans();

        $res = $this->insert($data);

        $id  = $this->getLastInsID();

        $this->updateSome($id,$data['uniacid'],$recommend,$choose);

        Db::commit();

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:配置编辑
     */
    public function startUpdate($dis,$data){

        $data['update_time'] = time();

        $recommend = $data['recommend'];

        unset($data['recommend']);

        $choose    = $data['choose'];

        unset($data['choose']);

        $data['imgs'] = !empty($data['imgs'])?implode(',',$data['imgs']):'';

        if(isset( $data['cate_id'])){

            $data['cate_id'] = array_pop($data['cate_id']);
        }

        Db::startTrans();

        $res = $this->where($dis)->update($data);

        $this->updateSome($data['id'],$data['uniacid'],$recommend,$choose);

        Db::commit();

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-23 15:34
     * @功能说明:
     */
    public function updateSome($id,$uniacid,$recommend,$choose){

        Db::name('longbing_card_web_star_recommend')->where(['star_id'=>$id])->delete();
//        dump($choose);exit;
        if(!empty($recommend)){

            foreach ($recommend as $value){

                $insert = [

                    'uniacid' => $uniacid,

                    'star_id' => $id,

                    'recommend_id' => $value
                ];

                Db::name('longbing_card_web_star_recommend')->insert($insert);
            }
        }


        foreach ($choose as $value){

            Db::name('longbing_card_web_star_connect')->where(['star_id'=>$id,'type'=>$value['type']])->delete();

            foreach ($value['list'] as $v){

                $insert = [

                    'uniacid' => $uniacid,

                    'star_id' => $id,

                    'connect_id' => $v,

                    'type'    => $value['type']
                ];

                Db::name('longbing_card_web_star_connect')->insert($insert);

            }

        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-23 16:14
     * @功能说明:回显的参数
     */
    public function returnInfo($data){

        $data['recommend'] = Db::name('longbing_card_web_star_recommend')->where(['star_id'=>$data['id']])->column('recommend_id');

        $data['recommend'] = array_values($data['recommend']);

        $type = [1,2,3,4];

        foreach ($type as $k=>$value){

            $data['choose'][$k]['type'] = $value;

            $dis = [

                'star_id' => $data['id'],

                'type'    => $value
            ];

            $data['choose'][$k]['list'] = Db::name('longbing_card_web_star_connect')->where($dis)->column('connect_id');

            $data['choose'][$k]['list'] = array_values($data['choose'][$k]['list']);
        }

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-25 14:54
     * @功能说明:推荐艺人
     */
    public function recommendList($star_id){

        $dis = [

            'b.star_id' => $star_id,

            'a.status'  => 1
        ];

        $data = $this->alias('a')
                ->join('longbing_card_web_star_recommend b','a.id = b.recommend_id')
                ->where($dis)
                ->field('a.cover,a.title,a.star,a.id,a.modular_id')
                ->group('a.id')
                ->order('a.top desc,a.id desc')
                ->select()
                ->toArray();

        return $data;
    }










}